import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ApiService } from 'src/app/core/api';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss'],
})
export class RegisterComponent implements OnInit {
    loginForm: FormGroup;
    sendCodeType = false;
    sendCodeNum = 10;
    _timer;

    ngOnInit() {
        this.loginForm = this.fb.group({
            mobile: [null,[Validators.required]],
            verifyCode: [null,[Validators.required]],
            reLoginPw: [null,[]],
            loginPw: [null, [Validators.required, Validators.pattern('(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,24}')]],
        });
    }
    submit() {
      if (this.loginForm.valid) {
        this.api.post('注册', this.loginForm.value).then((res: any) => {
          this.getUser({mobile:this.loginForm.value.mobile})
        }).catch((res) => {
          this.message.error(res.data.message);
        });
      }
    }
    toSendCode() {
      if (this.loginForm.value['mobile']) {
        this.api.post('发送手机验证码', { mobile: this.loginForm.value['mobile'], type: 'reg' }).then(res => {
          this.sendCodeNum = res['data']['durationTime'];
          // this.api.msgSuccess(this.msgData['register-code']);
          this.sendCodeType = true;
          this._timer = setInterval(() => {
            if (this.sendCodeNum == 0) {
              this.sendCodeType = false;
              clearInterval(this._timer);
            } else {
              this.sendCodeNum--;
            }
          }, 1000);
        });
      } else {
        // this.api.msgEeeor(this.msgData['register-mail-error']);
      }
    }
    getUser(data){
      this.router.navigate(['member/center/login']);
      // this.api.post('用户详情', data).then(res => {
      //   let userMap = res['data']['whiteHatUserDetail'];
      //   if(userMap['idcardAuthStatus'] == 'APPROVED'){
      //     userMap = Object.assign(userMap,{isSMRZ:true});
      //     this.router.navigate(['member/center/index']);
      //   }else{
      //     userMap = Object.assign(userMap,{isSMRZ:false});
      //     this.router.navigate(['pages/smrz']);
      //   };
      //   localStorage.setItem('user', JSON.stringify(userMap));
      // }).catch(res => {
      //   this.message.error(res.data.message);
      // });
    }
    constructor(private fb: FormBuilder, private router: Router, private api: ApiService, private message: NzMessageService) {}
}
